package com.cfjjc.gz.usraccount.service;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.common.consts.TradeInfoConst;
import com.cfjjc.gz.common.dao.DefaultDAO;
import com.cfjjc.gz.common.exception.OTSException;
import com.cfjjc.gz.common.service.BaseService;
import com.cfjjc.gz.common.util.DateTimeUtil;
import com.cfjjc.gz.common.util.RandomGUID;
import com.cfjjc.gz.common.util.StringUtil;
import com.cfjjc.gz.common.util.paged.PagedList;
import com.cfjjc.gz.usraccount.vo.A_USRVO;
import com.cfjjc.gz.usraccount.vo.A_USR_GRPVO;
import com.cfjjc.gz.usraccount.vo.A_USR_USRGRPVO;

/**
 * 
 * 说明：用户与用户组关系表 业务逻辑层对象
 * 
 * @author liuxun
 * @since 2012-03-06
 * @version 1.0
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class A_USR_USRGRPService extends BaseService{

	/**
	 * logger对象
	 */
	private static final Logger logger = Logger
			.getLogger(A_USR_USRGRPService.class);

	@Autowired
	private DefaultDAO dao;
	
	@Autowired
	private A_USRService a_USRService;

	@Autowired
	private A_USR_GRPService a_USR_GRPService;

	// 要访问的dao的命名空间
	private String namespace = "A_USR_USRGRP";

	public A_USR_USRGRPService() {
		super();
	}

	/**
	 * 仅查询单条用户与用户组关系表的详细信息
	 */
	public A_USR_USRGRPVO queryA_USR_USRGRP(A_USR_USRGRPVO param)
			throws OTSException {
		return (A_USR_USRGRPVO) dao.queryObject(param, this.namespace);
	}

	/**
	 * 根据条件检索用户与用户组关系表，返回分页结果
	 */
	public void queryA_USR_USRGRPs(A_USR_USRGRPVO param, PagedList records)
			throws OTSException {
		dao.queryPagedList(param, records, this.namespace);
	}

	/**
	 * 根据条件检索用户与用户组关系表，返回所有结果
	 */
	public List<A_USR_USRGRPVO> queryA_USR_USRGRPList(A_USR_USRGRPVO param)
			throws OTSException {
		return dao.queryList(param, this.namespace);
	}

	/**
	 * 根据某用户组，查询该用户组已选的用户列表信息
	 */
	public void querySelectedUsrList(A_USR_USRGRPVO param, PagedList records)
			throws OTSException {
		dao.queryPagedList(param, records, this.namespace, "querySelectedUsr");
	}

	/**
	 * 根据某用户组，查询该用户组已选的用户列表信息
	 */
	public void queryNotselectedUsrList(A_USR_USRGRPVO param, PagedList records)
			throws OTSException {
		dao.queryPagedList(param, records, this.namespace,
				"queryNotselectedUsr");
	}

	/**
	 * 增加用户与用户组关系表
	 */
	public void doInsertA_USR_USRGRP(A_USR_USRGRPVO param, String[] id)
			throws OTSException {

//		//查询用户组的访问控制权限
//		D_ACLVO queryUsrGrpAcl = new D_ACLVO();
//		queryUsrGrpAcl.setAuth_obj_type(TypeValueConst.USRGRP_AUTH);
//		queryUsrGrpAcl.setAuth_obj_id(param.getUsr_grp_id());
//		List queryUsrGrpAclList =  this.d_ACLService.queryD_ACLList(queryUsrGrpAcl);
		
		A_USR_GRPVO queryUsrGrp = new A_USR_GRPVO();
		queryUsrGrp.setUsr_grp_id(param.getUsr_grp_id());
		A_USR_GRPVO usrGrp = this.a_USR_GRPService.queryA_USR_GRP(queryUsrGrp);
		
		for (int i = 0; i < id.length; i++) {
			param.setKey(RandomGUID.getRandomGuid());
			param.setUsr_id(id[i]);
			param.setNode_id(SystemConst.NODE_ID);

			// 录入前的校验
			_checkFields(param, SystemConst.OPERATION_TYPE_NEW);
			
//			//查询用户的访问控制权限
//			D_ACLVO queryUsrAcl = new D_ACLVO();
//			queryUsrAcl.setAuth_obj_type(TypeValueConst.USR_AUTH);
//			queryUsrAcl.setAuth_obj_id(id[i]);
//			List queryUsrAclList =  this.d_ACLService.queryD_ACLList(queryUsrGrpAcl);
//			for (int j = 0; j < queryUsrGrpAclList.size(); j++) {
//				D_ACLVO usrGrpDacl = (D_ACLVO)queryUsrGrpAclList.get(j);
//				D_ACLVO usrGrp = new D_ACLVO();
//				usrGrp.setRsc_id(usrGrpDacl.getRsc_id());
//				usrGrp.setRsc_acc_id(usrGrpDacl.getRsc_acc_id());
//				usrGrp.setRsc_pro_id(usrGrpDacl.getRsc_pro_id());
//				
//				for (int k = 0; k < queryUsrAclList.size(); k++) {
//					D_ACLVO usrDacl = (D_ACLVO)queryUsrAclList.get(k);
//					D_ACLVO usr = new D_ACLVO();
//					usr.setRsc_id(usrDacl.getRsc_id());
//					usr.setRsc_acc_id(usrDacl.getRsc_acc_id());
//					usr.setRsc_pro_id(usrDacl.getRsc_pro_id());
//					if(usrGrp.equals(usr)){
//						throw new OTSException("");
//					}
//				}
//			}
			
//			A_USR_USRGRPVO query = new A_USR_USRGRPVO();
//			query.setUsr_grp_id(param.getUsr_grp_id());
//			query.setUsr_id(id[i]);
//			A_USR_USRGRPVO usr_usrgrp = this.queryA_USR_USRGRP(query);
//			if (usr_usrgrp != null) {
//				throw new OTSException("用户已经属于此用户组，请刷新页面重试！");
//			}
			
			List list = dao.queryList(param, namespace, "queryUsrSelectedRsc");
        	if(!list.isEmpty()){
        		HashMap<String, String> obj = (HashMap) list.get(0);
        		throw new OTSException("用户：" + obj.get("USR_NAME") + " 已挂接 " +obj.get("RSC_NAME") + " 资源的:" + obj.get("RSC_ACC") + " 账号！");
        	}
			
        	param.setCreate_at(DateTimeUtil.getDateTime());
			dao.insert(param, this.namespace);
			if (logger.isDebugEnabled()) {
				logger.debug("增加一条用户与用户组关系表记录:" + param);
			}
			
			A_USRVO queryUsr = new A_USRVO();
			queryUsr.setUsr_id(id[i]);
			A_USRVO usr = this.a_USRService.queryA_USR(queryUsr);
			
		//	insertTradeLog(TradeInfoConst.TRADE_ADD_USR_USRGRP, usr.getUsr_name() + "/" +usrGrp.getUsr_grp_name(), param);
		}
	}

	/**
	 * 修改用户与用户组关系表
	 */
	public void doUpdateA_USR_USRGRP(A_USR_USRGRPVO param) throws OTSException {

		A_USR_USRGRPVO query = new A_USR_USRGRPVO();
		query.setAuto_id(param.getAuto_id());
		A_USR_USRGRPVO res = this.queryA_USR_USRGRP(query);

		if (res == null) {
			throw new OTSException("要操作的记录不存在,请刷新页面后重试或联系管理员！");
		}
		param.setUpdate_at(DateTimeUtil.getDateTime());
		// 修改前的校验
		_checkFields(param, SystemConst.OPERATION_TYPE_MODIFY);
		dao.update(param, this.namespace);
		if (logger.isDebugEnabled()) {
			logger.debug("修改用户与用户组关系表:" + param);
		}
	}

	/**
	 * 删除用户与用户组关系表
	 */
	public void doDeleteA_USR_USRGRP(A_USR_USRGRPVO param) throws OTSException {
		// 删除前的校验
		_checkFields(param, SystemConst.OPERATION_TYPE_DELETE);

		A_USR_USRGRPVO query = new A_USR_USRGRPVO();
		query.setAuto_id(param.getAuto_id());
		A_USR_USRGRPVO res = this.queryA_USR_USRGRP(query);
		
		if (res == null) {
			throw new OTSException("要操作的记录不存在,请刷新页面后重试或联系管理员！");
		}

		dao.delete(param, this.namespace);
		if (logger.isDebugEnabled()) {
			logger.debug("删除用户与用户组关系表:" + param);
		}
		
	//	insertTradeLog(TradeInfoConst.TRADE_DEL_USR_USRGRP, "", res);
	}

	/** 以下为私有方法.私有方法应以下划线"_"开头 */
	/**
	 * 对当前的操作对象进行字段级的校验<br>
	 * 
	 * @param param
	 *            待操作的vo对象
	 * @param opeType
	 *            操作类型,见SystemConst中定义的常数
	 * 
	 * @see com.mofit.fs.yhbs.common.SystemConst
	 */
	private void _checkFields(A_USR_USRGRPVO param, int opeType)
			throws OTSException {
		if (logger.isDebugEnabled()) {
			logger.debug("对待保存的对象进行字段校验,操作类型opeType=" + opeType);
		}
		/*
		 * 除操作类型为insert外,主键字段不能为空
		 */
		if (opeType != SystemConst.OPERATION_TYPE_NEW) {

			if (param.getAuto_id() == null
					|| "".equals(String.valueOf(param.getAuto_id()))) {
				throw new OTSException("自动生成ID不能为空！");
			}

		}
		/*
		 * 对于操作类型为insert,非主键字段中不允许null的字段不能为空
		 */
		if (opeType == SystemConst.OPERATION_TYPE_NEW || opeType == SystemConst.OPERATION_TYPE_MODIFY) {

			if (param.getUsr_grp_id() == null
					|| "".equals(String.valueOf(param.getUsr_grp_id()))) {
				throw new OTSException("用户组ID不能为空！");
			}

			if (param.getUsr_id() == null
					|| "".equals(String.valueOf(param.getUsr_id()))) {
				throw new OTSException("用户ID不能为空！");
			}

			if (param.getNode_id() == null
					|| "".equals(String.valueOf(param.getNode_id()))) {
				throw new OTSException("节点ID不能为空！");
			}

		}

		/*
		 * 对于操作类型为insert或update,字段长度不能超过表中字段长度
		 */
		if (opeType == SystemConst.OPERATION_TYPE_NEW
				|| opeType == SystemConst.OPERATION_TYPE_MODIFY) {

			if (StringUtil.getLength(param.getUsr_grp_id()) > 50) {
				throw new OTSException("用户组ID内容长度不能大于50位,请检查！");
			}

			if (StringUtil.getLength(param.getUsr_id()) > 50) {
				throw new OTSException("用户ID内容长度不能大于50位,请检查！");
			}

			if (StringUtil.getLength(param.getNode_id()) > 50) {
				throw new OTSException("节点ID内容长度不能大于50位,请检查！");
			}

		}

		if (logger.isDebugEnabled()) {
			logger.debug("字段校验通过");
		}
	}
}